<?php

if (!function_exists('order_no')) {
    function order_no()
    {
        $order_id_main = date('YmdHis') . rand(10000000, 99999999);
        $order_id_len = strlen($order_id_main);
        $order_id_sum = 0;
        for ($i = 0; $i < $order_id_len; $i ++) {
            $order_id_sum += (int)(substr($order_id_main, $i, 1));
        }
        return $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT);
    }
}


if (!function_exists('is_mobile')) {
    function is_mobile()
    {
        if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
            return true;
        } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
            return true;
        } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
            return true;
        } elseif (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])) {
            return true;
        }
        return false;
    }
}

if (!function_exists('is_wechat')) {
    /**
     * 是否微信浏览器
     * @return bool
     */
    function is_wechat_browser()
    {
        return preg_match('/(wechatdevtools|MicroMessenger)/', $_SERVER['HTTP_USER_AGENT']) ? true : false;
    }
}

if (!function_exists('is_mini')) {
    function is_mini()
    {
        return preg_match('/servicewechat/', request()->header('referer')) ? true : false;
    }
}

if (!function_exists('epay_sign')) {
    /**
     * 易支付签名
     * @param $params
     * @param $key
     * @return string
     */
    function epay_sign($params, $key)
    {
        ksort($params);
        unset($params['sign']);
        unset($params['sign_type']);
        return md5(urldecode(http_build_query($params)) . $key);
    }
}


if (!function_exists('calculate_amount')) {
    function calculate_amount($total, $rate)
    {
        $rate = $rate / 1000;
        $amount = $total - ($total * $rate);
        return round($amount, 2);
    }
}
